<?php

$strAction = ( true == isset( $_GET['action'] ) && true == valStr( $_GET['action'] ) ) ? $_GET['action'] : NULL;

switch( $strAction ) {
	case 'insert':
		handleInsertData();
		break;
		
	case 'update':
		handleUpdateData();
		break;
		
	case 'delete':
		handleDeleteData();
		break;
		
	case 'quick_insert':
		handleQuickInsertData();
		break;
		
	default:
		display( 'Provided action not found' );
		exit;
		break;
}
exit;

function handleInsertData() {
	require_once 'classes/Base/BaseCasePapers.class.php';
	require_once 'classes/CasePapers.class.php';
	
// 	display( $_REQUEST );
// 	display( $_POST ); exit;
	
	if( false == isset( $_POST['Submit'] ) || false == valStr( $_POST['patient_id'] ) ) {
		$str = '';
		$str .= '<form name=myform method=post action=modules.php?file=diagnosis_types&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
		$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
		echo $str;
		exit;
	}
	
	if( true == isset( $_POST['operation'] ) && 'insert' == strtolower( $_POST['operation'] ) ) {
		$objCasePaper = new BaseCasePapers();
	
		$objCasePaper->setPatientId( $_POST['patient_id'] );
		$objCasePaper->setDiagnosisId( $_POST['diagnosis_id'] );
		$objCasePaper->setChargeTypeId( $_POST['charge_id'] );
		$objCasePaper->setSubmittedDate( 'now()' );
		$objCasePaper->setAmountPaid( $_POST['amount_paid'] );
		$objCasePaper->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
		$objCasePaper->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
	
		if( 1 == $objCasePaper->insert() ) {
			$str = '';
			$str .= '<form name=myform method=post action=modules.php?file=view_case_papers&type=view&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
			$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
			echo $str;
			exit;
		}
	}
	return true;
}

function handleUpdateData() {
	require_once 'classes/Base/BaseCasePapers.class.php';
	require_once 'classes/CasePapers.class.php';
	
	// display( $_REQUEST );
	// display( $_POST ); exit;
	
	if( false == isset( $_POST['Submit'] ) || false == valStr( $_POST['patient_id'] ) ) {
		$str = '';
		$str .= '<form name=myform method=post action=modules.php?file=diagnosis_types&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
		$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
		echo $str;
		exit;
	}
	
	if( true == isset( $_POST['operation'] ) && 'update' == strtolower( $_POST['operation'] ) ) {
		$objCasePapers 	= new CasePapers();
		$objCasePaper 	= $objCasePapers->fetchCasePapersById( $_POST['case_paper_id'] );
	
		$objCasePaper->setId( $_POST['case_paper_id'] );
	
		if( true == valStr( $_POST['patient_id'] ) ) {
			$objCasePaper->setPatientId( $_POST['patient_id'] );
		}
	
		if( true == valStr( $_POST['diagnosis_id'] ) ) {
			$objCasePaper->setDiagnosisId( $_POST['diagnosis_id'] );
		}
	
		if( true == valStr( $_POST['charge_id'] ) ) {
			$objCasePaper->setChargeTypeId( $_POST['charge_id'] );
		}
	
		if( true == valStr( $_POST['amount_paid'] ) ) {
			$objCasePaper->setAmountPaid( $_POST['amount_paid'] );
		}
	
		$objCasePaper->setUpdatedOn( 'now()' );
		$objCasePaper->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
	
		if( 1 == $objCasePaper->update() ) {
			$str = '';
			$str .= '<form name=myform method=post action=modules.php?file=view_case_papers&type=view&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
			$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
			echo $str;
			exit;
		}
	}
	return true;
}

function handleDeleteData() {
	
}

function handleQuickInsertData() {
	
	require_once 'classes/Base/BaseCasePapers.class.php';
	require_once 'classes/CasePapers.class.php';
	
	if( false == isset( $_GET['case_paper_id'] ) || false == isset( $_GET['charge_amount'] ) ) {
		echo 'Data sending failed.';
		exit;
	}
	
	$objCasePaper = new CasePapers();
	$objCasePaper = $objCasePaper->fetchCasePapersById( $_GET['case_paper_id'] );
	
	if( true == valObj( $objCasePaper, 'BaseCasePapers' ) ) {
		$objCasePaper->setAmountPaid( $_GET['charge_amount'] );
		$objCasePaper->setDiagnosisId( ( ( true == valStr( $objCasePaper->getDiagnosisId() ) ) ? $objCasePaper->getDiagnosisId() : NULL ) );
		$objCasePaper->setChargeTypeId( ( ( true == valStr( $objCasePaper->getChargeTypeId() ) ) ? $objCasePaper->getChargeTypeId() : NULL ) );
		
		if( 1 == $objCasePaper->update() ) {
			header( 'Location:modules.php?file=print_case_paper&type=print&case_paper_id=' . $_GET['case_paper_id'] . '&patient_id=' . $_GET['patient_id'] . '&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] );
			exit;
		} else {
			echo 'Case Paper upadte successfully.';
		}
	} else {
		echo 'Failed to load case paper.';
	}
}
?>